<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html 
     PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <title>Class: ActiveSupport::BufferedLogger</title>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  <meta http-equiv="Content-Script-Type" content="text/javascript" />
  <link rel="stylesheet" href="../.././rdoc-style.css" type="text/css" media="screen" />
  <script type="text/javascript">
  // <![CDATA[

  function popupCode( url ) {
    window.open(url, "Code", "resizable=yes,scrollbars=yes,toolbar=no,status=no,height=150,width=400")
  }

  function toggleCode( id ) {
    if ( document.getElementById )
      elem = document.getElementById( id );
    else if ( document.all )
      elem = eval( "document.all." + id );
    else
      return false;

    elemStyle = elem.style;
    
    if ( elemStyle.display != "block" ) {
      elemStyle.display = "block"
    } else {
      elemStyle.display = "none"
    }

    return true;
  }
  
  // Make codeblocks hidden by default
  document.writeln( "<style type=\"text/css\">div.method-source-code { display: none }</style>" )
  
  // ]]>
  </script>

</head>
<body>



    <div id="classHeader">
        <table class="header-table">
        <tr class="top-aligned-row">
          <td><strong>Class</strong></td>
          <td class="class-name-in-header">ActiveSupport::BufferedLogger</td>
        </tr>
        <tr class="top-aligned-row">
            <td><strong>In:</strong></td>
            <td>
                <a href="../../files/vendor/rails/activesupport/lib/active_support/buffered_logger_rb.html">
                vendor/rails/activesupport/lib/active_support/buffered_logger.rb
                </a>
        <br />
            </td>
        </tr>

        <tr class="top-aligned-row">
            <td><strong>Parent:</strong></td>
            <td>
                <a href="../Object.html">
                Object
               </a>
            </td>
        </tr>
        </table>
    </div>
  <!-- banner header -->

  <div id="bodyContent">



  <div id="contextContent">

    <div id="description">
      <p>
Inspired by the buffered logger idea by Ezra
</p>

    </div>


   </div>

    <div id="method-list">
      <h3 class="section-bar">Methods</h3>

      <div class="name-list">
      <a href="#M000816">add</a>&nbsp;&nbsp;
      <a href="#M000820">auto_flush</a>&nbsp;&nbsp;
      <a href="#M000817">auto_flushing=</a>&nbsp;&nbsp;
      <a href="#M000821">buffer</a>&nbsp;&nbsp;
      <a href="#M000822">clear_buffer</a>&nbsp;&nbsp;
      <a href="#M000819">close</a>&nbsp;&nbsp;
      <a href="#M000818">flush</a>&nbsp;&nbsp;
      <a href="#M000815">new</a>&nbsp;&nbsp;
      <a href="#M000814">silence</a>&nbsp;&nbsp;
      </div>
    </div>

  </div>


    <!-- if includes -->
    <div id="includes">
      <h3 class="section-bar">Included Modules</h3>

      <div id="includes-list">
        <span class="include-name"><a href="BufferedLogger/Severity.html">Severity</a></span>
      </div>
    </div>

    <div id="section">

    <div id="class-list">
      <h3 class="section-bar">Classes and Modules</h3>

      Module <a href="BufferedLogger/Severity.html" class="link">ActiveSupport::BufferedLogger::Severity</a><br />

    </div>

    <div id="constants-list">
      <h3 class="section-bar">Constants</h3>

      <div class="name-list">
        <table summary="Constants">
        <tr class="top-aligned-row context-row">
          <td class="context-item-name">MAX_BUFFER_SIZE</td>
          <td>=</td>
          <td class="context-item-value">1000</td>
        </tr>
        </table>
      </div>
    </div>



    <div id="attribute-list">
      <h3 class="section-bar">Attributes</h3>

      <div class="name-list">
        <table>
        <tr class="top-aligned-row context-row">
          <td class="context-item-name">auto_flushing</td>
          <td class="context-item-value">&nbsp;[R]&nbsp;</td>
          <td class="context-item-desc"></td>
        </tr>
        <tr class="top-aligned-row context-row">
          <td class="context-item-name">level</td>
          <td class="context-item-value">&nbsp;[RW]&nbsp;</td>
          <td class="context-item-desc"></td>
        </tr>
        </table>
      </div>
    </div>
      


    <!-- if method_list -->
    <div id="methods">
      <h3 class="section-bar">Public Class methods</h3>

      <div id="method-M000815" class="method-detail">
        <a name="M000815"></a>

        <div class="method-heading">
          <a href="#M000815" class="method-signature">
          <span class="method-name">new</span><span class="method-args">(log, level = DEBUG)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000815-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000815-source">
<pre>
    <span class="ruby-comment cmt"># File vendor/rails/activesupport/lib/active_support/buffered_logger.rb, line 37</span>
37:     <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">initialize</span>(<span class="ruby-identifier">log</span>, <span class="ruby-identifier">level</span> = <span class="ruby-constant">DEBUG</span>)
38:       <span class="ruby-ivar">@level</span>         = <span class="ruby-identifier">level</span>
39:       <span class="ruby-ivar">@buffer</span>        = {}
40:       <span class="ruby-ivar">@auto_flushing</span> = <span class="ruby-value">1</span>
41:       <span class="ruby-ivar">@guard</span> = <span class="ruby-constant">Mutex</span>.<span class="ruby-identifier">new</span>
42: 
43:       <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">log</span>.<span class="ruby-identifier">respond_to?</span>(<span class="ruby-identifier">:write</span>)
44:         <span class="ruby-ivar">@log</span> = <span class="ruby-identifier">log</span>
45:       <span class="ruby-keyword kw">elsif</span> <span class="ruby-constant">File</span>.<span class="ruby-identifier">exist?</span>(<span class="ruby-identifier">log</span>)
46:         <span class="ruby-ivar">@log</span> = <span class="ruby-identifier">open</span>(<span class="ruby-identifier">log</span>, (<span class="ruby-constant">File</span><span class="ruby-operator">::</span><span class="ruby-constant">WRONLY</span> <span class="ruby-operator">|</span> <span class="ruby-constant">File</span><span class="ruby-operator">::</span><span class="ruby-constant">APPEND</span>))
47:         <span class="ruby-ivar">@log</span>.<span class="ruby-identifier">sync</span> = <span class="ruby-keyword kw">true</span>
48:       <span class="ruby-keyword kw">else</span>
49:         <span class="ruby-constant">FileUtils</span>.<span class="ruby-identifier">mkdir_p</span>(<span class="ruby-constant">File</span>.<span class="ruby-identifier">dirname</span>(<span class="ruby-identifier">log</span>))
50:         <span class="ruby-ivar">@log</span> = <span class="ruby-identifier">open</span>(<span class="ruby-identifier">log</span>, (<span class="ruby-constant">File</span><span class="ruby-operator">::</span><span class="ruby-constant">WRONLY</span> <span class="ruby-operator">|</span> <span class="ruby-constant">File</span><span class="ruby-operator">::</span><span class="ruby-constant">APPEND</span> <span class="ruby-operator">|</span> <span class="ruby-constant">File</span><span class="ruby-operator">::</span><span class="ruby-constant">CREAT</span>))
51:         <span class="ruby-ivar">@log</span>.<span class="ruby-identifier">sync</span> = <span class="ruby-keyword kw">true</span>
52:         <span class="ruby-ivar">@log</span>.<span class="ruby-identifier">write</span>(<span class="ruby-value str">&quot;# Logfile created on %s&quot;</span> <span class="ruby-operator">%</span> [<span class="ruby-constant">Time</span>.<span class="ruby-identifier">now</span>.<span class="ruby-identifier">to_s</span>])
53:       <span class="ruby-keyword kw">end</span>
54:     <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <h3 class="section-bar">Public Instance methods</h3>

      <div id="method-M000816" class="method-detail">
        <a name="M000816"></a>

        <div class="method-heading">
          <a href="#M000816" class="method-signature">
          <span class="method-name">add</span><span class="method-args">(severity, message = nil, progname = nil, &amp;block)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000816-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000816-source">
<pre>
    <span class="ruby-comment cmt"># File vendor/rails/activesupport/lib/active_support/buffered_logger.rb, line 56</span>
56:     <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">add</span>(<span class="ruby-identifier">severity</span>, <span class="ruby-identifier">message</span> = <span class="ruby-keyword kw">nil</span>, <span class="ruby-identifier">progname</span> = <span class="ruby-keyword kw">nil</span>, <span class="ruby-operator">&amp;</span><span class="ruby-identifier">block</span>)
57:       <span class="ruby-keyword kw">return</span> <span class="ruby-keyword kw">if</span> <span class="ruby-ivar">@level</span> <span class="ruby-operator">&gt;</span> <span class="ruby-identifier">severity</span>
58:       <span class="ruby-identifier">message</span> = (<span class="ruby-identifier">message</span> <span class="ruby-operator">||</span> (<span class="ruby-identifier">block</span> <span class="ruby-operator">&amp;&amp;</span> <span class="ruby-identifier">block</span>.<span class="ruby-identifier">call</span>) <span class="ruby-operator">||</span> <span class="ruby-identifier">progname</span>).<span class="ruby-identifier">to_s</span>
59:       <span class="ruby-comment cmt"># If a newline is necessary then create a new message ending with a newline.</span>
60:       <span class="ruby-comment cmt"># Ensures that the original message is not mutated.</span>
61:       <span class="ruby-identifier">message</span> = <span class="ruby-node">&quot;#{message}\n&quot;</span> <span class="ruby-keyword kw">unless</span> <span class="ruby-identifier">message</span>[<span class="ruby-value">-1</span>] <span class="ruby-operator">==</span> <span class="ruby-value">?\n</span>
62:       <span class="ruby-identifier">buffer</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">message</span>
63:       <span class="ruby-identifier">auto_flush</span>
64:       <span class="ruby-identifier">message</span>
65:     <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M000817" class="method-detail">
        <a name="M000817"></a>

        <div class="method-heading">
          <a href="#M000817" class="method-signature">
          <span class="method-name">auto_flushing=</span><span class="method-args">(period)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Set the auto-<a href="BufferedLogger.html#M000818">flush</a> period. Set to
true to <a href="BufferedLogger.html#M000818">flush</a> after every log
message, to an integer to <a href="BufferedLogger.html#M000818">flush</a>
every N messages, or to false, nil, or zero to never auto-<a
href="BufferedLogger.html#M000818">flush</a>. If you turn auto-flushing
off, be sure to regularly <a href="BufferedLogger.html#M000818">flush</a>
the log yourself &#8212; it will eat up memory until you do.
</p>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000817-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000817-source">
<pre>
    <span class="ruby-comment cmt"># File vendor/rails/activesupport/lib/active_support/buffered_logger.rb, line 84</span>
84:     <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">auto_flushing=</span>(<span class="ruby-identifier">period</span>)
85:       <span class="ruby-ivar">@auto_flushing</span> =
86:         <span class="ruby-keyword kw">case</span> <span class="ruby-identifier">period</span>
87:         <span class="ruby-keyword kw">when</span> <span class="ruby-keyword kw">true</span>;                <span class="ruby-value">1</span>
88:         <span class="ruby-keyword kw">when</span> <span class="ruby-keyword kw">false</span>, <span class="ruby-keyword kw">nil</span>, <span class="ruby-value">0</span>;       <span class="ruby-constant">MAX_BUFFER_SIZE</span>
89:         <span class="ruby-keyword kw">when</span> <span class="ruby-constant">Integer</span>;             <span class="ruby-identifier">period</span>
90:         <span class="ruby-keyword kw">else</span> <span class="ruby-identifier">raise</span> <span class="ruby-constant">ArgumentError</span>, <span class="ruby-node">&quot;Unrecognized auto_flushing period: #{period.inspect}&quot;</span>
91:         <span class="ruby-keyword kw">end</span>
92:     <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M000819" class="method-detail">
        <a name="M000819"></a>

        <div class="method-heading">
          <a href="#M000819" class="method-signature">
          <span class="method-name">close</span><span class="method-args">()</span>
          </a>
        </div>
      
        <div class="method-description">
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000819-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000819-source">
<pre>
     <span class="ruby-comment cmt"># File vendor/rails/activesupport/lib/active_support/buffered_logger.rb, line 107</span>
107:     <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">close</span>
108:       <span class="ruby-identifier">flush</span>
109:       <span class="ruby-ivar">@log</span>.<span class="ruby-identifier">close</span> <span class="ruby-keyword kw">if</span> <span class="ruby-ivar">@log</span>.<span class="ruby-identifier">respond_to?</span>(<span class="ruby-identifier">:close</span>)
110:       <span class="ruby-ivar">@log</span> = <span class="ruby-keyword kw">nil</span>
111:     <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M000818" class="method-detail">
        <a name="M000818"></a>

        <div class="method-heading">
          <a href="#M000818" class="method-signature">
          <span class="method-name">flush</span><span class="method-args">()</span>
          </a>
        </div>
      
        <div class="method-description">
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000818-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000818-source">
<pre>
     <span class="ruby-comment cmt"># File vendor/rails/activesupport/lib/active_support/buffered_logger.rb, line 94</span>
 94:     <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">flush</span>
 95:       <span class="ruby-ivar">@guard</span>.<span class="ruby-identifier">synchronize</span> <span class="ruby-keyword kw">do</span>
 96:         <span class="ruby-keyword kw">unless</span> <span class="ruby-identifier">buffer</span>.<span class="ruby-identifier">empty?</span>
 97:           <span class="ruby-identifier">old_buffer</span> = <span class="ruby-identifier">buffer</span>
 98:           <span class="ruby-ivar">@log</span>.<span class="ruby-identifier">write</span>(<span class="ruby-identifier">old_buffer</span>.<span class="ruby-identifier">join</span>)
 99:         <span class="ruby-keyword kw">end</span>
100: 
101:         <span class="ruby-comment cmt"># Important to do this even if buffer was empty or else @buffer will</span>
102:         <span class="ruby-comment cmt"># accumulate empty arrays for each request where nothing was logged.</span>
103:         <span class="ruby-identifier">clear_buffer</span>
104:       <span class="ruby-keyword kw">end</span>
105:     <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M000814" class="method-detail">
        <a name="M000814"></a>

        <div class="method-heading">
          <a href="#M000814" class="method-signature">
          <span class="method-name">silence</span><span class="method-args">(temporary_level = ERROR) {|self| ...}</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Silences the logger for the duration of the block.
</p>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000814-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000814-source">
<pre>
    <span class="ruby-comment cmt"># File vendor/rails/activesupport/lib/active_support/buffered_logger.rb, line 21</span>
21:     <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">silence</span>(<span class="ruby-identifier">temporary_level</span> = <span class="ruby-constant">ERROR</span>)
22:       <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">silencer</span>
23:         <span class="ruby-keyword kw">begin</span>
24:           <span class="ruby-identifier">old_logger_level</span>, <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">level</span> = <span class="ruby-identifier">level</span>, <span class="ruby-identifier">temporary_level</span>
25:           <span class="ruby-keyword kw">yield</span> <span class="ruby-keyword kw">self</span>
26:         <span class="ruby-keyword kw">ensure</span>
27:           <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">level</span> = <span class="ruby-identifier">old_logger_level</span>
28:         <span class="ruby-keyword kw">end</span>
29:       <span class="ruby-keyword kw">else</span>
30:         <span class="ruby-keyword kw">yield</span> <span class="ruby-keyword kw">self</span>
31:       <span class="ruby-keyword kw">end</span>
32:     <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <h3 class="section-bar">Protected Instance methods</h3>

      <div id="method-M000820" class="method-detail">
        <a name="M000820"></a>

        <div class="method-heading">
          <a href="#M000820" class="method-signature">
          <span class="method-name">auto_flush</span><span class="method-args">()</span>
          </a>
        </div>
      
        <div class="method-description">
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000820-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000820-source">
<pre>
     <span class="ruby-comment cmt"># File vendor/rails/activesupport/lib/active_support/buffered_logger.rb, line 114</span>
114:       <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">auto_flush</span>
115:         <span class="ruby-identifier">flush</span> <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">buffer</span>.<span class="ruby-identifier">size</span> <span class="ruby-operator">&gt;=</span> <span class="ruby-ivar">@auto_flushing</span>
116:       <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M000821" class="method-detail">
        <a name="M000821"></a>

        <div class="method-heading">
          <a href="#M000821" class="method-signature">
          <span class="method-name">buffer</span><span class="method-args">()</span>
          </a>
        </div>
      
        <div class="method-description">
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000821-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000821-source">
<pre>
     <span class="ruby-comment cmt"># File vendor/rails/activesupport/lib/active_support/buffered_logger.rb, line 118</span>
118:       <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">buffer</span>
119:         <span class="ruby-ivar">@buffer</span>[<span class="ruby-constant">Thread</span>.<span class="ruby-identifier">current</span>] <span class="ruby-operator">||=</span> []
120:       <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M000822" class="method-detail">
        <a name="M000822"></a>

        <div class="method-heading">
          <a href="#M000822" class="method-signature">
          <span class="method-name">clear_buffer</span><span class="method-args">()</span>
          </a>
        </div>
      
        <div class="method-description">
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000822-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000822-source">
<pre>
     <span class="ruby-comment cmt"># File vendor/rails/activesupport/lib/active_support/buffered_logger.rb, line 122</span>
122:       <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">clear_buffer</span>
123:         <span class="ruby-ivar">@buffer</span>.<span class="ruby-identifier">delete</span>(<span class="ruby-constant">Thread</span>.<span class="ruby-identifier">current</span>)
124:       <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>


    </div>


  </div>


<div id="validator-badges">
  <p><small><a href="http://validator.w3.org/check/referer">[Validate]</a></small></p>
</div>

</body>
</html>